package org.example.zookeeper.consumer;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryForever;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.data.Stat;
import org.example.zookeeper.config.ZkProperties;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class ChildrenNodeFetcher {
    private List<String> getChildren(CuratorFramework client, String path) throws Exception {
        try {
            Stat stat = client.checkExists().forPath(path);
            if (stat == null || stat.getNumChildren() == 0) {
                return null;
            }
            List<String> services = client.getChildren().forPath(path);
            return services;
        } catch (KeeperException.NoNodeException e) {
            return null;
        }
    }

    public Map<String, List<String>> fetchChildrenDepth2(ZkProperties zkConfig, String path) throws Exception {
        if (path == null || path.length() == 0) {
            throw new RuntimeException("请传入父路径");
        }
        RetryPolicy retryPolicy = new RetryForever(1000);//new ExponentialBackoffRetry(1000, 3);
        CuratorFramework client = CuratorFrameworkFactory
                .newClient(zkConfig.getServer(), zkConfig.getSessionTimeout(), zkConfig.getConnectionTimeout(), retryPolicy);
        client.start();
        try {
            Map<String, List<String>> result = new LinkedHashMap<>();
            List<String> children = getChildren(client, path);
            if (children != null) {
                for (String child : children) {
                    result.put(child, getChildren(client, path + "/" + child));
                }
            }
            return result;
        } finally {
            if (client != null) {
                client.close();
            }
        }
    }

    public List<String> fetchChildren(ZkProperties zkConfig, String path) throws Exception {
        if (path == null || path.length() == 0) {
            throw new RuntimeException("请传入父路径");
        }
        RetryPolicy retryPolicy = new RetryForever(1000);//new ExponentialBackoffRetry(1000, 3);
        CuratorFramework client = CuratorFrameworkFactory
                .newClient(zkConfig.getServer(), zkConfig.getSessionTimeout(), zkConfig.getConnectionTimeout(), retryPolicy);
        client.start();
        try {
            List<String> children = getChildren(client, path);
            return children;
        } finally {
            if (client != null) {
                client.close();
            }
        }
    }

    public Map<String, List<String>> fetchChildren(ZkProperties zkConfig, List<String> paths) throws Exception {
        if (paths == null || paths.size() == 0) {
            throw new RuntimeException("请传入父路径");
        }
        RetryPolicy retryPolicy = new RetryForever(1000);//new ExponentialBackoffRetry(1000, 3);
        CuratorFramework client = CuratorFrameworkFactory
                .newClient(zkConfig.getServer(), zkConfig.getSessionTimeout(), zkConfig.getConnectionTimeout(), retryPolicy);
        client.start();
        try {
            Map<String, List<String>> map = new LinkedHashMap<>();
            for (String path : paths) {
                List<String> children = getChildren(client, path);
                map.put(path, children);
            }
            return map;
        } finally {
            if (client != null) {
                client.close();
            }
        }
    }

//    private void saveRawConsumers(String service, List<String> consumers) {
//        FileWriter writer = null;
//        try {
//            Path path = Paths.get(this.outputDir, service + ".txt");
//            writer = new FileWriter(path.toFile());
//            for (String consumer : consumers) {
//                writer.write(consumer);
//                writer.write('\n');
//            }
//        } catch (IOException ex) {
//            ex.printStackTrace();
//        } finally {
//            if (writer != null) {
//                try {
//                    writer.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//    }
}
